package cn.com.huak.www.sell.controller;

import cn.com.huak.www.sell.dataObject.ProductCategory;
import cn.com.huak.www.sell.dataObject.ProductInfo;
import cn.com.huak.www.sell.dto.OrderDTO;
import cn.com.huak.www.sell.exception.ProductException;
import cn.com.huak.www.sell.form.ProductForm;
import cn.com.huak.www.sell.service.ProductCategoryService;
import cn.com.huak.www.sell.service.ProductInfoService;
import cn.com.huak.www.sell.utils.KeyUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.UUID;

/**
 * @author huak@wanggang
 * @date 2019/2/20 22:00
 */
@Controller
@RequestMapping("/seller/product")
@Slf4j
public class SellerProductController {

    @Resource(name="productInfoServiceImpl")
    private ProductInfoService productInfoService;

    @Resource(name="productCategoryServiceImpl")
    private ProductCategoryService productCategoryService;

    @RequestMapping("/list")
    public String list(@RequestParam(value="page", defaultValue = "1") Integer page,
                       @RequestParam(value="size", defaultValue = "10") Integer size,
                       Model model){
        //1. 查询订单列表
        PageRequest pageRequest = new PageRequest(page - 1, size);
        Page<ProductInfo> list = productInfoService.findAll(pageRequest);

        //2. 将订单列表放在model中
        model.addAttribute("page", list);
        model.addAttribute("currentPage", page);
        model.addAttribute("size", size);

        //3. 返回结果
        return "product/list";
    }

    /**
     * 卖家端商品上架
     * @param productId 商品id
     * @param model
     * @return
     */
    @RequestMapping("/on_sale")
    public String onSale(@RequestParam("productId") String productId,
                       Model model){
        try{
            //1. 上架
            productInfoService.onSale(productId);

            //2. 提示信息
            model.addAttribute("msg", "上架成功");
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/success";
        }catch (ProductException e){
            log.error("【卖家端上架商品】失败原因:{}", e.getMessage());
            model.addAttribute("message", e.getMessage());
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/error";
        }
    }

    /**
     * 卖家端商品下架
     * @param productId 商品id
     * @param model
     * @return
     */
    @RequestMapping("/off_sale")
    public String offSale(@RequestParam("productId") String productId,
                         Model model){
        try{
            //1. 下架
            productInfoService.offSale(productId);

            //2. 提示信息
            model.addAttribute("msg", "下架成功");
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/success";
        }catch (ProductException e){
            log.error("【卖家端下架商品】失败原因:{}", e.getMessage());
            model.addAttribute("message", e.getMessage());
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/error";
        }
    }

    /**
     * 商品添加/修改页面
     * @param productId 商品id
     * @param model
     * @return
     */
    @GetMapping(value = "/index")
    public String index(@RequestParam(value="productId", required = false) String productId,
                        Model model){
        //1. productId 不为空 修改操作 查询当前的商品
        if(!StringUtils.isEmpty(productId)){
            ProductInfo one = productInfoService.findOne(productId);
            model.addAttribute("product", one);
        }

        //2. 查询所有的类目
        List<ProductCategory> all = productCategoryService.findAll();
        model.addAttribute("categoryList", all);

        //3. 返回视图
        return "product/index";
    }

    /**
     * 商品信息保存
     * @param productForm
     * @param bindingResult
     * @param model
     * @return
     */
    @PostMapping(value = "/save")
    public String save(@Valid ProductForm productForm,
                       BindingResult bindingResult,
                       Model model){
        //1. 表单校验
        if(bindingResult.hasErrors()){
            model.addAttribute("msg", bindingResult.getFieldError().getDefaultMessage());
            model.addAttribute("url", "/sell/seller/product/index");
            return "common/error";
        }

        try{
            //2. 属性的复制
            ProductInfo productInfo = new ProductInfo();
            if(!StringUtils.isEmpty(productForm.getId())){ // 为空表示表示新增 不为空表示修改
                productInfo = productInfoService.findOne(productForm.getId());
            }
            BeanUtils.copyProperties(productForm, productInfo);
            if(org.springframework.util.StringUtils.isEmpty(productForm.getId())){
                productInfo.setId(KeyUtil.genKey());
            }

            //3. 数据的实例化
            productInfoService.save(productInfo);
            model.addAttribute("msg", "操作成功");
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/success";
        }catch (ProductException e){
            model.addAttribute("msg", e.getMessage());
            model.addAttribute("url", "/sell/seller/product/list");
            return "common/error";
        }
    }
}
